package com.three.usermanagement.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.Role;
import com.three.core.vo.R;
import com.three.usermanagement.mapper.RoleMapper;
import com.three.usermanagement.service.RoleService;
import com.three.usermanagement.vo.RoleVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/userManagement/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMapper roleMapper;
    //分页查询角色
    @PostMapping("/selectRoleByPage")
    public R<Page<Role>> selectRoleByPage(@RequestBody RoleVo roleVo){
        return roleService.selectRoleByPage(roleVo);
    }
    //添加角色
    @PostMapping("/addRole")
    public R addRole(@RequestParam String username, @RequestBody Role role) {
        // 处理添加角色的逻辑
        role.setCreateBy(username);
        //创建时间为现在
        role.setCreateTime(LocalDateTime.now());
        int insert = roleMapper.insert(role);
        if (insert > 0){
            return new R<>(200, "添加成功", null);
        }else {
            return new R<>(500, "添加失败", null);
        }
    }
    //修改角色信息
    @PostMapping("/updateRoleInfo")
    public R updateRoleInfo(@RequestParam String username, @RequestBody Role role) {
        // 处理添加角色的逻辑
        role.setUpdateBy(username);
        //创建修改时间为现在
        role.setUpdateTime(LocalDateTime.now());
        int insert = roleMapper.updateById(role);
        if (insert > 0){
            return new R<>(200, "修改成功",null);
        }else {
            return new R<>(500, "修改失败", null);
        }
    }
    //删除角色
    @GetMapping("/deleteRole")
    public R deleteRole(@RequestParam Integer roleId) {
        // 处理添加角色的逻辑
        return roleService.deleteRoleById(roleId);
    }
    @GetMapping("/selectAllRole")
    public R selectAllRole() {
        // 处理添加角色的逻辑
        List<Role> role  = roleMapper.selectList(null);
        if (role != null){
            return new R<>(200, "查询成功", role);
        }else {
            return new R<>(500, "查询失败", null);
        }
    }
    @GetMapping("/selectRole")
    public R selectRole(@Param("userId") Integer id) {
        return roleService.selectUserRole(id);
    }
    @PostMapping("/updateRole")
    public R updateRole(Integer userId, Integer[] roleIds) {
        // 处理添加角色的逻辑
       return roleService.updateRole(userId, roleIds);
    }
}
